package cst.a931xmc.yuejian20.Fragment.Home;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cst.a931xmc.yuejian20.Adapter.ActivityAdapter;
import cst.a931xmc.yuejian20.Bean.ActivityBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ActivityModel;
import cst.a931xmc.yuejian20.R;

public class ActivityFragment extends Fragment {
    private RecyclerView recyclerView;
    private ActivityAdapter adapter;
    private GridLayoutManager layoutManager;
    private Listener<List<ActivityBean>> listener;
    private ActivityModel model;
    private static final int MAX = 8;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_hactivity,container,false);
        initViews(view);
        initData();
        model=new ActivityModel();
        model.getList(listener);
        return view;
    }

    public void initViews(View view){
        recyclerView = view.findViewById(R.id.rv_activity);
        adapter = new ActivityAdapter(getActivity());
        layoutManager = new GridLayoutManager(getActivity(),2);
        recyclerView.setLayoutManager(layoutManager);
    }

    public void initData(){
        listener=new Listener<List<ActivityBean>>() {
            @Override
            public void onResponse(final List<ActivityBean> list) {
                layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        if(list.get(position).getActivity_name().length()>MAX)
                            return 2;
                        return 1;
                    }
                });
                recyclerView.setAdapter(adapter);
                adapter.setList(list);
            }

            @Override
            public void onFail(String Massage) {

            }
        };
    }
}
